using DG.Tweening;
using System.Threading.Tasks;
using UnityEngine;
using System;
using TEngine;

namespace GameLogic
{

    public static class UIAnimationHelper
    {
        /// <summary>
        /// 执行UI动画
        /// </summary>
        /// <param name="window">目标窗口</param>
        /// <param name="action">动画动作</param>
        /// <param name="animationType">动画类型</param>
        /// <param name="onStart">动画开始回调</param>
        /// <param name="onComplete">动画完成回调</param>
        public static async Task DoTweenAnimation(
            UIWindow window, 
            AnimationAction action, 
            AnimationType animationType, 
            Action onStart = null, 
            Action onComplete = null)
        {
            var tcs = new TaskCompletionSource<bool>();
            var rectTransform = window.rectTransform;
            if (rectTransform == null) return;

            switch (animationType)
            {
                case AnimationType.Fade:
                    HandleFadeAnimation(window, action, rectTransform, onStart, () =>
                    {
                        onComplete?.Invoke();
                        tcs.SetResult(true);
                    });
                    break;
                case AnimationType.Scale:
                    HandleScaleAnimation(window, action, rectTransform, onStart, () =>
                    {
                        onComplete?.Invoke();
                        tcs.SetResult(true);
                    });
                    break;
                case AnimationType.Slide:
                    HandleSlideAnimation(window, action, rectTransform, onStart, () =>
                    {
                        onComplete?.Invoke();
                        tcs.SetResult(true);
                    });
                    break;
            }

            await tcs.Task;
        }

        private static void HandleFadeAnimation(
            UIWindow window, 
            AnimationAction action, 
            RectTransform rectTransform, 
            Action onStart, 
            Action onComplete)
        {
            var canvasGroup = window.gameObject.GetComponent<CanvasGroup>();
            if (canvasGroup == null)
            {
                canvasGroup = window.gameObject.AddComponent<CanvasGroup>();
            }

            float targetAlpha = action == AnimationAction.Open ? 1f : 0f;
            float startAlpha = action == AnimationAction.Open ? 0f : 1f;

            canvasGroup.alpha = startAlpha;
            canvasGroup.DOFade(targetAlpha, 0.5f)
                .OnStart(() => onStart?.Invoke())
                .OnComplete(() => onComplete?.Invoke());
        }

        private static void HandleScaleAnimation(
            UIWindow window, 
            AnimationAction action, 
            RectTransform rectTransform, 
            Action onStart, 
            Action onComplete)
        {
            Vector3 targetScale = action == AnimationAction.Open ? Vector3.one : Vector3.zero;
            Vector3 startScale = action == AnimationAction.Open ? Vector3.zero : Vector3.one;

            rectTransform.localScale = startScale;
            rectTransform.DOScale(targetScale, 0.5f)
                .SetEase(Ease.OutBack)
                .OnStart(() => onStart?.Invoke())
                .OnComplete(() => onComplete?.Invoke());
        }

        private static void HandleSlideAnimation(
            UIWindow window, 
            AnimationAction action, 
            RectTransform rectTransform, 
            Action onStart, 
            Action onComplete)
        {
            Vector2 startPos = rectTransform.anchoredPosition;
            Vector2 targetPos = action == AnimationAction.Open ? Vector2.zero : new Vector2(0, -1000);

            rectTransform.anchoredPosition = action == AnimationAction.Open ? new Vector2(0, 1000) : Vector2.zero;
            rectTransform.DOAnchorPos(targetPos, 0.5f)
                .SetEase(Ease.OutQuint)
                .OnStart(() => onStart?.Invoke())
                .OnComplete(() => onComplete?.Invoke());
        }
    }

    
    public enum AnimationAction
    {
        Open,   // 打开动画
        Close   // 关闭动画
    }

    public enum AnimationType
    {
        Fade,    // 淡入淡出
        Scale,   // 缩放
        Slide    // 滑动
    }
}